package com.dp.mallchat.common.chat.controller;


import com.dp.mallchat.common.chat.domain.vo.req.ContactFriendReq;
import com.dp.mallchat.common.chat.domain.vo.resp.ChatRoomResp;
import com.dp.mallchat.common.chat.service.ContactService;
import com.dp.mallchat.common.chat.service.RoomService;
import com.dp.mallchat.common.common.domain.vo.req.CursorPageBaseReq;
import com.dp.mallchat.common.common.domain.vo.req.IdReqVO;
import com.dp.mallchat.common.common.domain.vo.resp.ApiResult;
import com.dp.mallchat.common.common.domain.vo.resp.CursorPageBaseResp;
import com.dp.mallchat.common.common.utils.RequestHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会话列表 前端控制器
 * </p>
 *
 * @author <a href="https://gitee.com/baiDP">adu</a>
 * @since 2024-06-27
 */
@RestController
@RequestMapping("/capi/chat")
@Api(value = "会话列表", tags = "会话列表")
public class ContactController {

    @Autowired
    private ContactService contactService;
    @Autowired
    private RoomService roomService;

    /**
     * 实现大致流程
     *  1、场景一：用户未登录，仅返回热门房间信息
     *  2、场景二：用户已登录，返回热门房间信息，加上用户加入的房间信息
     *      1、获取用户基础房间信息
     *      2、获取用户热门房间信息
     *      3、聚合基础房间和热门房间
     *      4、构建附属信息
     *          4.1、获取群头像、名称等基本信息
     *          4.2、获取会话列表列表中最新一条消息，并获取是谁发的，发的什么内容，再获取用户在每个会话的未读数
     *      5、返回数据
     * @param request
     * @return
     */
    @GetMapping("/public/contact/page")
    @ApiOperation("会话列表")
    public ApiResult<CursorPageBaseResp<ChatRoomResp>> getContactPage(@Valid CursorPageBaseReq request){
        Long uid = RequestHolder.getThreadLocal().getUid();
        return ApiResult.success(contactService.getContactPage(request, uid));
    }


    @GetMapping("/public/contact/detail")
    @ApiOperation("会话详情")
    public ApiResult<ChatRoomResp> getContactDetail(@Valid IdReqVO request) {
        Long uid = RequestHolder.getThreadLocal().getUid();
        return ApiResult.success(roomService.getContactDetail(uid, request.getId()));
    }

    @GetMapping("/public/contact/detail/friend")
    @ApiOperation("会话详情(联系人列表发消息用)")
    public ApiResult<ChatRoomResp> getContactDetailByFriend(@Valid ContactFriendReq request) {
        Long uid = RequestHolder.getThreadLocal().getUid();
        return ApiResult.success(roomService.getContactDetailByFriend(uid, request.getUid()));
    }
}

